#!/bin/bash

DATE=`date +%Y%m%d-%H%M`;
HOST=`uname -n`;
USER=postgres
BKPDIR=/var/backups/postgresql
BKPFILE=pg_${HOST}_backup.tar.gz

#DATABASE=( "agcrises" "asprev" "igeopr" );

DATABASE[0]="agcrises"
DATABASE[1]="asprev"
DATABASE[2]="igeopr"

#echo ${DATABASE[0]}
#echo ${DATABASE[2]}
#echo ${#DATABASE[@]}

export DATE HOST USER BKPDIR FILE DATABASE

echo $HOST

echo cd $BKPDIR/
cd $BKPDIR/;

echo ------------------------ check pwd `pwd` ----------------------------------------

echo "rm [ -e ${BKPFILE} ] && rm ${BKPFILE}"

[ -e ${BKPFILE} ] && rm ${BKPFILE};

for ((i=0; i<${#DATABASE[@]}; i++));
  do
        #echo ${DATABASE[$i]}
        #teste criação pg_dump
        #echo pg_${DATABASE[$i]}_${HOST}_${DATE}.backup > pg_${DATABASE[$i]}_${HOST}_${DATE}.backup

        #gera pg_dump dos database
        echo "pg_dump -vh ${HOST} -U ${USER} ${DATABASE[$i]} >> pg_${DATABASE[$i]}_${HOST}_${DATE}.backup"
        pg_dump -h ${HOST} -U ${USER} ${DATABASE[$i]} >> pg_${DATABASE[$i]}_${HOST}_${DATE}.backup
  done;
#Cria tar.bz com arquivos.backup
tar cvzf ${BKPFILE} pg_*_${HOST}_${DATE}.backup

#remove arquivos.backup
echo rm -f *.backup
rm -f *.backup

cd -;
unset DATE HOST USER BKPDIR FILE DATABASE;
